package com.jxw.cloudpen.web.netty.handler;

import com.jxw.cloudpen.web.netty.Entity.StreamData;
import com.jxw.cloudpen.web.netty.common.CommonRpcDecoderHandler;
import com.jxw.cloudpen.web.netty.common.CommonRpcEncoderHandler;
import com.jxw.cloudpen.web.netty.common.LocalSession;
import com.jxw.cloudpen.web.netty.common.SessionManger;
import com.jxw.cloudpen.web.netty.util.ByteObjUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ConnectionHandler extends ChannelInboundHandlerAdapter {
    private static final Logger log = LoggerFactory.getLogger(ConnectionHandler.class);
    /**
     * 收到消息
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        LocalSession session = new LocalSession(ctx.channel());
        ctx.channel().attr(LocalSession.SESSION_KEY).set(session);
        SessionManger.addLocalSession(session);

        ctx.pipeline().remove(ConnectionHandler.this);
        ctx.pipeline().addLast("decoder", new CommonRpcDecoderHandler());
        ctx.pipeline().addLast("encoder", new CommonRpcEncoderHandler());
        //心跳
//                p.addLast("heartBeat",new HeartBeatServerHandler());
        ctx.pipeline().addLast("loginRequest",new LoginRequestHandler());
        ctx.pipeline().addLast(new BusinHandler());
        StreamData result = new StreamData();
        result.setClientId(session.getClientId());
        result.setType(1);
        ctx.channel().writeAndFlush(ByteObjUtil.objToBytes(result));


    }


}
